//
//  AYMDoctorController.m
//  QuickCure
//
//  Created by kangkang on 16/5/6.
//  Copyright © 2016年 ayumi. All rights reserved.
//

#import "AYMDoctorController.h"

#import "AYMTumorModel.h"

#import "AYMDoctorsCell.h"

#import "AYMTumorDoctorInfoController.h"

@interface AYMDoctorController ()

@property(nonatomic,strong)NSMutableArray *doctorsArray;

@property(nonatomic,strong)NSArray *allArray;


@end

@implementation AYMDoctorController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self loadWithDown];
    
    [self setupUI];
    
}

# pragma mark -
# pragma mark - setupUI
-(void)setupUI{
    
    self.navigationItem.title = @"为您匹配到的医生";
    
    UINib *nib = [UINib nibWithNibName:@"Doctors" bundle:[NSBundle mainBundle]];
    
    [self.tableView registerNib:nib forCellReuseIdentifier:@"doccell"];
    
    self.tableView.rowHeight = 100;
    
    [self addRefreshAndPull];
    
}

# pragma mark -
# pragma mark - 添加下拉和上拉控件
-(void)addRefreshAndPull{
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadWithUp)];
    
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadWithDown)];
    
    
}

# pragma mark -
# pragma mark - 下拉刷新
-(void)loadWithDown{
 
    [self loadDataWithPullDown:YES];

}

# pragma mark -
# pragma mark - 上拉加载
-(void)loadWithUp{
   
    [self loadDataWithPullDown:NO];

}

# pragma mark -
# pragma mark - 网络请求数据
-(void)loadDataWithPullDown:(BOOL)isPullingDown{
    
    
    NSArray *array = [AYMSQLiteManager queryTumor];
    
    if (array.count != 0) {
        
        self.allArray = array;
        
        [self.tableView reloadData];
        
        [self.tableView.mj_footer endRefreshing];
        
        [self.tableView.mj_header endRefreshing];
        
    }else{
        
        NSString *urlStr = @"http://iosapi.itcast.cn/matchDoctors.json.php";
        
        NSDictionary *param = @{
                                
                                @"ci1_id":@1,
                                @"ci2_id":@3,
                                @"ci3_id":@3,
                                @"diagnosis_type":@0,
                                @"page_size":@15,
                                @"is_confirmed":@1,
                                @"user_id":@1000089,
                                @"page":@1,
                                @"has_diagnosis":@2
                                
                                };
        
        [[AYMNetworkTool sharedManager] request:RequestMethodPost urlString:urlStr parameters:param finished:^(NSDictionary *responseObject, NSError *error) {
            
            NSArray *datas = responseObject[@"data"];
            
            Log(@"%@",responseObject);
            
            NSMutableArray *temrray = [NSMutableArray array];
            
            for (NSDictionary *dict in datas) {
                
                AYMTumorModel *model = [[AYMTumorModel alloc] init];
                
                [model yy_modelSetWithDictionary:dict];
                
                [temrray addObject:model];
                
                [AYMSQLiteManager insertTumorWithModel:model];
                
            }
            
            self.allArray = temrray;
            
             Log(@"%@",temrray[0]);

            
            [self.tableView reloadData];
            
            
            [self.tableView.mj_footer endRefreshing];
            
            [self.tableView.mj_header endRefreshing];
            
            Log(@"---------------------==============================");
            

            
        }];
        

           }
    
    }



# pragma mark -
# pragma mark - 数据源，代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.allArray.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    AYMDoctorsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"doccell" forIndexPath:indexPath];
    
    AYMTumorModel *model = self.allArray[indexPath.row];
    
    cell.model = model;
    
    return  cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
       AYMTumorDoctorInfoController *focusDetailVc = [[AYMTumorDoctorInfoController alloc] init];
    
    [self.navigationController pushViewController:focusDetailVc animated:YES];
    
    
}

@end
